;
(function() {
    new Vue({
        el: '#customer-detail',
        data: {
            navData: ['客户信息', '意向信息', '关联合同'],
            navActive: '客户信息',
            cmrId: hlUrlUtil.GetQueryString('id'),
            cmrData: [],
            cmrDataAt: [],
            cmrType: [{ name: '公司', type: 'QY' }, { name: '个人', type: 'PE' }],
            getTypeData: [],
            getChannel: [{
                    name: '自行上门',
                    type: 'SM'
                },
                {
                    name: '中介',
                    type: 'ZJ'
                },
                {
                    name: '老客户推荐',
                    type: 'TJ'
                },
                {
                    name: '互联网平台',
                    type: 'NT'
                },
                {
                    name: '内部推荐',
                    type: 'NJ'
                },
                {
                    name: '其他',
                    type: 'ET'
                }
            ],
            customerStateAt: [{
                    name: '潜在客户',
                    type: 'PO'
                },
                {
                    name: '意向客户',
                    type: 'YX'
                },
                {
                    name: '流失客户',
                    type: 'LS'
                },
                {
                    name: '成交客户',
                    type: 'CJ'
                }
            ],
            blacklist: [{ name: '是', type: 'Y' }, { name: '否', type: 'N' }],
            editCmrFlag: false,
            followUpFlag: false,
            followData: null, //跟进记录
            associateData: [], //关联合同
            yxFlag: false,
            editYxFlag: false,
            editYxIndex: -1,
            addYxIndex: 0,
            bounced: {
                unitFlag: false,
                wpFlag: false
            },
            unitData: [],
            wpData: [],
            // 选定项目下的原始单元及工位数据
            projects: {
                list: [],
                builddata: [],
                floorsdata: [],
                unitdata: [],
                stWpdata: [],
                id: null,
                projectName: null,
                buildingId: null,
                floorId: null,
                unitId: null
            },
            // 已选过的单元及工位数据
            projectInfo: {
                units: [],
                wpdata: [],
            },
            getname: {
                buildname: null,
                unitname: null,
                floorname: null,
                stop_acreage: null
            },
            // 新增意向数据
            addWillData: {
                type: {
                    list: [{ name: '整租', type: 'ER' }, { name: '工位', type: 'WR' }],
                    active: 'ER'
                },
                acreage: null,
                st_count: null,
                wp_count: null,
                project_id: null,
                project_name: null
            },
            // 意向信息列表数据
            willsList: [],
            // 意向信息列表中确定添加的单元及工位数据
            willsConfirmedInfoList: [{
                'unitData': [],
                'wpData': []
            }],
            typeArcms: {
                radio: [],
                check: [],
                input: [],
                text: []
            },
            oldTypeArcms: { radio: [], check: [], input: [], text: [] },
            isShowddBtn: false,
            ddAccount: null,
            isShowQyLayer: false,
            isShowAccountLayer: false,
            corpId: null,
            corpSecret: null,
        },
        filters: {
            formatDate: function(time) {
                if (time === undefined) return '--'
                return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd')
            },
            formatNum: function(item) {
                if (item === undefined) return '--'
                return hlNumberUtil.formatNum(item);
            },
            cmrType: function(str) {
                var result;
                switch (str) {
                    case 'QY':
                        result = '企业类型';
                        break;
                    case 'PE':
                        result = '个人类型';
                        break;
                    default:
                        result = ''
                }
                return result;
            },
            industryType: function(str) {
                return hlCodeConvert.industryType(str)
            },
            channelType: function(str) {
                return hlCodeConvert.customerChannelType(str)
            },
            customerState: function(str) {
                return hlCodeConvert.customerState(str)
            },
            auditState: function(str) {
                return hlCodeConvert.contractAuditState(str)
            },
            formatUndefine: function(item) {
                if (item === undefined) return '--'
                return item
            }
        },
        methods: {
            checkRadio: function(i, val) {
                var flag = false,
                    index0 = null;
                this.typeArcms.check[i].act.map(function(item, index) {
                    if (val == item) {
                        flag = true;
                        index0 = index;
                    }
                })
                if (flag) {
                    this.typeArcms.check[i].act.splice(index0, 1)
                    return
                }
                this.typeArcms.check[i].act.push(val)
            },
            getlistZdy: function(data) {
                var self = this;
                self.typeArcms = { radio: [], check: [], input: [], text: [] }
                hlhttp.get({
                    url: 'customer/field_config',
                    success: function(res) {
                        res.map(function(item) {
                                //组装各类型自定义字段
                                if (item.configType == 'C') {
                                    if (item.fieldType == 'radio') {
                                        item.act = item.fieldRange.split(',')[0]
                                        self.typeArcms.radio.push(item)
                                    }
                                    if (item.fieldType == 'checkbox') {
                                        item.act = []
                                        self.typeArcms.check.push(item)
                                    }
                                    if (item.fieldType == 'tel' || item.fieldType == 'email') {
                                        item.act = ''
                                        self.typeArcms.input.push(item)
                                    }
                                    if (item.fieldType == 'text') {
                                        item.act = ''
                                        self.typeArcms.text.push(item)
                                    }
                                }
                            })
                            //根据code确定当前选择
                        data.map(function(item) {
                            self.typeArcms.radio.map(function(item1) {
                                if (item.configCode == item1.configCode) {
                                    item1.act = item.configValue ? item.configValue : ''
                                }
                            })
                            self.typeArcms.check.map(function(item1) {
                                if (item.configCode == item1.configCode) {
                                    item1.act = item.configValue ? item.configValue.split(',') : []
                                }
                            })
                            self.typeArcms.input.map(function(item1) {
                                if (item.configCode == item1.configCode) {
                                    item1.act = item.configValue ? item.configValue : ''
                                }
                            })
                            self.typeArcms.text.map(function(item1) {
                                if (item.configCode == item1.configCode) {
                                    item1.act = item.configValue ? item.configValue : ''
                                }
                            })
                        })
                        self.oldTypeArcms = JSON.parse(JSON.stringify(self.typeArcms));
                    }
                })
            },
            //导航切换
            navTab: function(name) {
                this.navActive = name;
                if (name == '关联合同') {
                    this.getRelevantContract()
                }
                if (name === '意向信息') {
                    this.getCustomerWillsData()
                }
            },
            //获取详情
            getCustomer: function() {
                var _this = this;
                hlhttp.get({
                    url: 'customer,records',
                    data: {
                        customer_id: _this.cmrId
                    },
                    success: function(res) {
                        _this.cmrData = res;
                        _this.cmrDataAt = JSON.parse(JSON.stringify(_this.cmrData));
                        _this.getTypeData.map(function(item) {
                            if (item.dictCode == _this.cmrDataAt.industry) {
                                _this.cmrDataAt.ityName = item.dictName;
                                _this.cmrData.ityName = item.dictName;
                            }
                        })
                        _this.getlistZdy(res.customerFields)
                        delete _this.cmrDataAt.records;
                    }
                })
            },
            // 获取行业
            getType: function() {
                var _this = this;
                hlhttp.get({
                    url: 'configure/industry/dict',
                    success: function(res) {
                        _this.getTypeData = res;
                        _this.getCustomer()
                    }
                })
            },
            //设置所属行业
            setindustryType: function(type) {
                this.cmrDataAt.industry = type;
            },
            //设置获取渠道
            setchannelType: function(type) {
                this.cmrDataAt.channelType = type;
            },
            //设置状态
            setcustomerState: function(type) {
                this.cmrDataAt.customerState = type;
            },
            //设置客户类型
            setcmrDataAtType: function(type) {
                if (type === this.cmrDataAt.type) return false;
                if (type !== this.cmrDataAt.type) {
                    this.cmrDataAt.type = type;
                }
            },
            //设置黑名单
            setblackState: function(type) {
                this.cmrDataAt.blackState = type;
            },
            // 验证客户
            validateCustomer: function() {
                if (!this.cmrDataAt.name) {
                    hl.toast('请填写客户名称')
                    return false;
                }
                if (this.cmrDataAt.email) {
                    if (!hlValidateUtil.validateEmail(this.cmrDataAt.email)) {
                        hl.toast('邮箱格式不符合')
                        return false;
                    }
                }
                return true;
            },
            //保存招商客户
            saveCosumter: function() {
                var _this = this;
                if (!this.validateCustomer()) return false;

                var flagCheck = false,
                    CheckName = [],
                    custom_fields = [],
                    flagInput = false,
                    InputName = [],
                    flagText = false,
                    textName = [],
                    flagText1 = false,
                    textName1 = [],
                    flagRadio = false,
                    radioName = [];
                this.typeArcms.radio.map(function(item) {
                    if (item.isValid == 'true') {
                        if (item.isRequired == 'true') {
                            if (!item.act.length) {
                                flagRadio = true;
                                radioName.push(item.displayName);
                            }
                        }
                        custom_fields.push({ config_code: item.configCode, config_value: item.act })
                    }
                })
                this.typeArcms.check.map(function(item) {
                    if (item.isValid == 'true') {
                        if (item.isRequired == 'true') {
                            if (!item.act.length) {
                                flagCheck = true;
                                CheckName.push(item.displayName);
                            }
                        }
                        custom_fields.push({ config_code: item.configCode, config_value: item.act.toString() || '#' })
                    }
                })
                this.typeArcms.input.map(function(item) {
                    if (item.isValid == 'true') {
                        if (item.isRequired == 'true') {
                            if (!item.act.replace(/^\s+|\s+$/g, '').length) {
                                flagInput = true;
                                InputName.push(item.displayName);
                            }
                        }
                        custom_fields.push({ config_code: item.configCode, config_value: item.act || '#' })
                    }
                })
                this.typeArcms.text.map(function(item) {
                    if (item.isValid == 'true') {
                        if (item.isRequired == 'true') {
                            if (!item.act.replace(/^\s+|\s+$/g, '').length) {
                                flagText = true;
                                textName.push(item.displayName);
                            }
                            if (item.act.replace(/^\s+|\s+$/g, '').length > item.fieldRange) {
                                flagText1 = true;
                                textName1.push(item.displayName + '的字数不能超过' + item.fieldRange + '个');
                            }
                        }
                        custom_fields.push({ config_code: item.configCode, config_value: item.act || '#' })
                    }
                })
                if (flagRadio) {
                    hl.toast('请选择' + radioName[0])
                    return false;
                }
                if (flagCheck) {
                    hl.toast('请选择' + CheckName[0])
                    return false;
                }
                if (flagInput) {
                    hl.toast('请填写' + InputName[0])
                    return false;
                }
                if (flagText) {
                    hl.toast('请填写' + textName[0])
                    return false;
                }
                if (flagText1) {
                    hl.toast(textName1[0])
                    return false;
                }

                hlhttp.put({
                    url: 'customer',
                    data: {
                        customer_id: _this.cmrId,
                        type: _this.cmrDataAt.type,
                        name: _this.cmrDataAt.name || '#',
                        industry: _this.cmrDataAt.industry || '#',
                        lperson: _this.cmrDataAt.lperson || '#',
                        phone: _this.cmrDataAt.phone || '#',
                        channel_type: _this.cmrDataAt.channelType,
                        channel_name: _this.cmrDataAt.channelName || '#',
                        customer_state: _this.cmrDataAt.customerState,
                        black_state: _this.cmrDataAt.blackState,
                        memo: _this.cmrDataAt.memo || '#',
                        idcode: _this.cmrDataAt.idcode || '#',
                        email: _this.cmrDataAt.email || '#',
                        customer_fields: JSON.stringify(custom_fields)
                    },
                    success: function() {
                        hl.toast('编辑成功')
                        _this.getCustomer()
                        _this.typeArcms = { radio: [], check: [], input: [], text: [] }
                        _this.editCmrFlag = false;
                    }
                })
            },
            //客户信息返回
            backCmr: function() {
                this.editCmrFlag = false;
                this.cmrDataAt = JSON.parse(JSON.stringify(this.cmrData));
                delete this.cmrDataAt.records;
            },
            //跟进记录
            postFollow: function() {
                var _this = this;
                if (!Sdate.value) {
                    hl.toast('请选择时间')
                    return
                }
                if (!this.followData) {
                    hl.toast('请填写备注')
                    return
                }
                hlhttp.post({
                    url: 'customer/records',
                    data: {
                        memo: _this.followData,
                        record_date: Sdate.value,
                        customer_id: _this.cmrId
                    },
                    success: function() {
                        _this.followUpFlag = false;
                        Sdate.setAttribute('value', '')
                        _this.followData = null;
                        _this.getCustomer()
                    }
                })
            },
            //back
            backFollow: function() {
                this.followUpFlag = false;
                Sdate.setAttribute('value', '')
                this.followData = null;
            },
            //获取关联合同
            getRelevantContract: function() {
                var _this = this;
                hlhttp.get({
                    url: 'zlf/contracts',
                    data: { zlf_id: _this.cmrId },
                    success: function(res) {
                        _this.associateData = res
                    }
                })
            },
            //获取签约项目
            getProjects: function() {
                var self = this;
                hlhttp.get({
                    url: 'user/projects',
                    success: function(data) {
                        self.projects.list = data;
                    }
                })
            },
            getProjectsId: function(id, name) {
                var self = this;
                self.projects.id = id;
                self.projects.projectName = name;
                self.addWillData.project_id = id;
                self.addWillData.project_name = name;
            },
            //获取楼宇  n == 1 单元  n == 2 工位
            getBuildings: function(n) {
                this.projects.id = this.addWillData.project_id
                if (!this.addWillData.project_id) {
                    hl.toast('请先选择意向项目')
                    return false;
                }
                var self = this;
                if (n == 1) this.bounced.unitFlag = true;
                if (n == 2) this.bounced.wpFlag = true;

                hlhttp.get({
                    url: 'project/buildings',
                    data: { project_id: self.projects.id },
                    success: function(res) {
                        self.projects.builddata = res
                    }
                })
            },
            //获取楼宇id:
            getBuildId: function(id, name) {
                this.projects.buildingId = id;
                this.getname.buildname = name;
                this.projects.stWpdata = [];
                $('.floor-select span, .unit-select span, .building-select span').text('请选择')
                var _this = this;
                hlhttp.get({
                    url: 'building/floors',
                    data: {
                        project_id: _this.projects.id,
                        building_id: id,
                    },
                    success: function(res) {
                        _this.projects.unitdata = [];
                        if (res.length === 0) {
                            hl.toast('暂无楼层，请重新选择')
                            return false
                        }
                        _this.projects.floorsdata = res
                    }
                })
            },
            //查询
            queryUnit: function() {
                if (!this.projects.floorId) return;
                var _this = this;
                hlhttp.get({
                    url: 'floor/units',
                    data: {
                        project_id: _this.projects.id,
                        floor_id: _this.projects.floorId,
                        building_id: _this.projects.buildingId
                    },
                    success: function(res) {
                        if (res.length === 0) {
                            hl.toast('暂无单元，请重新选择')
                            return false
                        }
                        _this.projects.unitdata = res
                    }
                })
            },
            //获取楼层下的单元
            getUnits: function(id, name) {
                this.projects.floorId = id;
                this.getname.floorname = name;
                var _this = this;
                this.projects.unitdata = [];
                $('.last-unit span').removeClass('activeNot active')
                $('.unit-select span').text('请选择')
                hlhttp.get({
                    url: 'floor/units',
                    data: {
                        project_id: _this.projects.id,
                        floor_id: _this.projects.floorId,
                        building_id: _this.projects.buildingId
                    },
                    success: function(res) {
                        if (res.length === 0) {
                            hl.toast('暂无单元，请重新选择')
                            return false
                        }
                        _this.projects.unitdata = res
                        setTimeout(function() {
                            _this.selectUnit()
                        }, 200);
                    }
                })
            },
            //勾选单元
            selectUnit: function() {
                var _this = this;
                // 原已添加过的单元灰色标记
                for (var i = 0; i < this.projectInfo.units.length; i++) {
                    $('.unit-active span').each(function(index, el) {
                        if ($(el).attr('data-unitid') == _this.projectInfo.units[i].unit_id) {
                            $(el).addClass('activeNot')
                        }
                    });
                }
            },
            //选中取消单元
            selUnit: function(e, i, id) {
                var data = this.projects.unitdata;
                if ($(e.srcElement).hasClass('activeNot')) {
                    hl.toast('无法操作选择过的单元')
                    return
                }
                // 选中单元
                if ($(e.srcElement).hasClass('active') === false) {
                    $(e.srcElement).addClass('active')
                    this.projectInfo.units.push({
                        building_id: data[i].buildingId,
                        building_name: this.getname.buildname,
                        floor_id: data[i].floorId,
                        floor_num: this.getname.floorname,
                        unit_id: data[i].unitId,
                        unit_name: data[i].unitName
                    })
                    return false;
                }
                // 取消单元
                if ($(e.srcElement).hasClass('active')) {
                    $(e.srcElement).removeClass('active')
                    for (var j = 0; j < this.projectInfo.units.length; j++) {
                        if (this.projectInfo.units[j].unit_id == id) {
                            this.projectInfo.units.splice(j, 1)
                        }
                    }
                    return false;
                }
            },
            // 确认添加单元或者工位
            confirmAddUnitsOrWp: function(n) {
                // 添加单元
                if (n == 1) {
                    this.unitData = JSON.parse(JSON.stringify(this.projectInfo.units));
                    this.bounced.unitFlag = false;
                    this.clearOriginalProMes()
                }
                // 添加工位
                if (n == 2) {
                    this.wpData = JSON.parse(JSON.stringify(this.projectInfo.wpdata));
                    this.bounced.wpFlag = false;
                    this.clearOriginalProMes()
                }
            },
            // 关闭单元弹窗
            closeAddUnitPop: function() {
                this.bounced.unitFlag = false;
                this.clearOriginalProMes()
            },
            // 关闭工位弹窗
            closeAddwpPop: function() {
                this.bounced.wpFlag = false;
                this.clearOriginalProMes()
            },
            //获取车位工位单元
            getStWpUnitsList: function(id, name) {
                var _this = this;
                this.projects.floorId = id;
                this.getname.floorname = name;
                $('.unit-select span').text('请选择')
                this.projects.unitdata = [];
                this.projects.stWpdata = []
                hlhttp.get({
                    url: 'floor/units',
                    data: {
                        project_id: _this.projects.id, //项目ID
                        floor_id: id, //楼层ID
                        building_id: _this.projects.buildingId
                    },
                    success: function(res) {
                        _this.projects.unitdata = res;
                    }
                })
            },
            //获取工位
            getWp: function(id, name, index) {
                var _this = this;
                this.projects.unitId = id;
                this.getname.unitname = name;
                this.uIndex = index;
                $('.st-unit span').removeClass('activeNot active')
                hlhttp.get({
                    url: 'units/positions',
                    data: { unit_id: id },
                    success: function(res) {
                        _this.projects.stWpdata = res;
                        setTimeout(function() {
                            _this.selectUnitWp()
                        }, 200);
                    }
                })
            },
            // 标记已选择过的工位
            selectUnitWp: function() {
                var _this = this;
                for (var w = 0; w < this.projectInfo.wpdata.length; w++) {
                    var wpAct = this.projectInfo.wpdata[w].positions;
                    $('.wp-list span').each(function(index, el) {
                        for (var wp = 0; wp < wpAct.length; wp++) {
                            if ($(el).attr('data-posId') == wpAct[wp].position_id) {
                                $(el).addClass('activeNot')
                            }
                        }
                    });
                }
            },
            // 勾选工位
            selectWp: function(e, i, id, name) {
                var data = this.projects.unitdata;
                if ($(e.srcElement).hasClass('activeNot')) {
                    hl.toast('无法操作选择过的工位')
                    return
                }

                // 选中工位
                if (!$(e.srcElement).hasClass('active')) {
                    $(e.srcElement).addClass('active')

                    if (!this.projectInfo.wpdata.length) {
                        this.projectInfo.wpdata.push({
                            building_id: this.projects.buildingId,
                            building_name: this.getname.buildname,
                            floor_id: this.projects.floorId,
                            floor_num: this.getname.floorname,
                            unit_id: this.projects.unitId,
                            unit_name: this.getname.unitname,
                            positions: [{
                                position_id: id, //车位ID
                                position_name: name
                            }],
                        })
                    } else {
                        var flag = false;
                        index = null;
                        for (var j = 0; j < this.projectInfo.wpdata.length; j++) {
                            if (this.projectInfo.wpdata[j].unit_id == this.projects.unitId) {
                                index = j;
                                flag = 1;
                            } else {
                                index = j;
                                flag = 2;
                            }
                        }
                        if (flag == 1) {
                            this.projectInfo.wpdata[index].positions.push({ position_id: id, position_name: name })
                        }
                        if (flag == 2) {
                            this.projectInfo.wpdata.push({
                                building_id: this.projects.buildingId,
                                building_name: this.getname.buildname,
                                floor_id: this.projects.floorId,
                                floor_num: this.getname.floorname,
                                unit_id: this.projects.unitId,
                                unit_name: this.getname.unitname,
                                positions: [{ position_id: id, position_name: name }],
                            })
                        }
                    }

                    return false;
                }
                // 取消工位
                if ($(e.srcElement).hasClass('active')) {
                    $(e.srcElement).removeClass('active')
                    for (var j = 0; j < this.projectInfo.wpdata.length; j++) {
                        if (this.projectInfo.wpdata[j].positionId == id) {
                            this.projectInfo.wpdata.splice(j, 1)
                        }
                    }
                    return false;
                }
            },
            //删除单元
            deleteUnit: function(i) {
                this.unitData.splice(i, 1)
                this.projectInfo.units = this.unitData;
            },
            //删除已选的工位
            delwp: function(i, n) {
                this.wpData[i].positions.splice(n, 1)
                if (!this.wpData[i].positions.length) {
                    this.wpData.splice(i, 1)
                }
                this.projectInfo.wpdata = JSON.parse(JSON.stringify(this.wpData));
            },
            // 清空原选择的单元或者工位信息(但未确认添加过，即原始数据)
            clearOriginalProMes: function(n) {
                var self = this;
                var clear = function() {
                    self.projects.unitdata = [];
                    self.projects.floorsdata = [];
                    self.projects.stWpdata = [];
                    self.projects.buildingId = null;
                    self.projects.unitId = null;
                    // self.projectInfo.wpdata = JSON.parse(JSON.stringify(self.wpData));
                    self.getname = { buildname: null, unitname: null, floorname: null, stop_acreage: null };
                    $('.floor-select span, .unit-select span, .building-select span').text('请选择')
                }
                clear()
            },
            // 清空原添加过的单元或者工位信息
            clearAddedMes: function() {
                var data = {
                    units: [],
                    wpdata: []
                }
                this.projectInfo = data;
            },
            // 添加意向
            addWill: function() {

                if (this.yxFlag === true) hl.toast('请先保存已添加的意向信息')
                if (this.yxFlag === false) {
                    if (this.editYxFlag === false) {
                        this.yxFlag = true
                        return false
                    }
                    if (this.editYxFlag === true) {
                        hl.toast('请先取消或者保存先前编辑操作')
                        return false
                    }
                }
            },
            // 取消意向
            cancelWill: function() {
                var self = this;
                var data = {
                        'type': {
                            list: [{ name: '整租', type: 'ER' }, { name: '工位', type: 'WR' }],
                            active: 'ER'
                        },
                        'acreage': null,
                        'st_count': null,
                        'wp_count': null,
                        'project_id': null,
                        'project_name': null
                    }
                    // $('#beginTime').attr('value','');
                    // $('#endTime').attr('value','');
                self.$set(self.$data, 'addWillData', data)
                self.unitData = [];
                self.wpData = [];
                self.yxFlag = false;
                self.editYxFlag = false;
                self.editYxIndex = -1;
                self.clearOriginalProMes()
                self.clearAddedMes()
            },
            // 保存意向
            saveWill: function(type, willId) {
                var self = this;
                if (!self.validateYxMes()) {
                    return false;
                }
                // 保存数据
                var YxData = function() {
                    var data = {
                        'type': self.addWillData.type.active,
                        'xmin_date': $('.beginTime').attr('value'),
                        'xmax_date': $('.endTime').attr('value'),
                        'acreage': self.addWillData.acreage || '#',
                        'st_count': self.addWillData.st_count || '#',
                        'wp_count': self.addWillData.wp_count || '#',
                        'customer_id': self.cmrId,
                        'project_id': self.addWillData.project_id,
                        'project_name': self.addWillData.project_name
                    }

                    var willArr = [];
                    if (data.type === 'ER') {
                        willArr = self.projectInfo.units;
                        willArr.map(function(_item) {
                            _item.positions = '#'
                        })

                    }
                    if (data.type === 'WR') {
                        willArr = self.projectInfo.wpdata;
                    }
                    data.will = JSON.stringify(willArr)

                    return data
                }
                var sendAddYxRequest = function() {
                    hlhttp.post({
                        url: 'customer/wills',
                        data: YxData(),
                        success: function(data) {
                            hl.toast('保存成功')
                            self.yxFlag = false
                            self.getCustomerWillsData()
                            self.cancelWill()
                        }
                    })
                }
                var sendEditYxRequest = function() {
                    hlhttp.put({
                        url: 'customer/wills/' + willId,
                        data: YxData(),
                        success: function(data) {
                            hl.toast('编辑成功')
                            self.editYxFlag = false;
                            self.editYxIndex = -1;
                            self.getCustomerWillsData()
                            self.cancelWill()
                        }
                    })
                }
                if (type === 'add') {
                    sendAddYxRequest()
                }
                if (type === 'edit') {
                    sendEditYxRequest()
                }
            },
            // 删除意向
            deleteWill: function(id) {
                var self = this;
                hlhttp.delete({
                    url: 'customer/wills/' + id,
                    success: function(data) {
                        hl.toast('删除成功')
                        self.getCustomerWillsData()
                    }
                })
            },
            // 编辑意向
            editWill: function(i) {
                var self = this;
                if (self.editYxFlag === true) {
                    hl.toast('请先取消或者保存先前编辑操作')
                    return false
                }
                if (self.yxFlag === true) {
                    hl.toast('请先取消或者保存先前新增操作')
                    return false
                }

                self.editYxFlag = true;
                self.editYxIndex = i;
                // 基本信息
                self.addWillData.type = {
                    list: [{ name: '整租', type: 'ER' }, { name: '工位', type: 'WR' }],
                    active: self.willsList[i].type
                }
                self.addWillData.acreage = self.willsList[i].acreage;
                self.addWillData.st_count = self.willsList[i].stCount;
                self.addWillData.wp_count = self.willsList[i].wpCount;
                self.addWillData.project_id = self.willsList[i].projectId;
                self.addWillData.project_name = self.willsList[i].projectName;
                // 单元工位信息
                self.unitData = self.willsConfirmedInfoList[i].unitData;
                self.wpData = self.willsConfirmedInfoList[i].wpData;
                self.projectInfo.units = JSON.parse(JSON.stringify(self.unitData))
                self.projectInfo.wpdata = JSON.parse(JSON.stringify(self.wpData))
                    // 项目信息
                    // 时间
                setTimeout(function() {
                    $('.beginTime').attr('value', hlDateUtil.formatUTCDate(self.willsList[i].xminDate, 'yyyy-MM-dd'))
                    $('.endTime').attr('value', hlDateUtil.formatUTCDate(self.willsList[i].xmaxDate, 'yyyy-MM-dd'))
                }, 200)



            },
            // 选择意向类型
            selectYxType: function(type) {
                if (type == 'ER') {
                    this.addWillData.type.active = 'ER'
                }
                if (type == 'WR') {
                    this.addWillData.type.active = 'WR'
                }
            },
            // 验证意向信息
            validateYxMes: function() {
                var self = this;
                if (!$('.beginTime').attr('value')) {
                    hl.toast('请选择开始时间')
                    return false
                }
                if (!$('.endTime').attr('value')) {
                    hl.toast('请选择结束时间')
                    return false
                }
                if (hlDateUtil.shellDate($('.beginTime').attr('value')) > hlDateUtil.shellDate($('.endTime').attr('value'))) {
                    hl.toast('结束时间不能早于开始时间')
                    $('#endTime').attr('value', '')
                    return false
                }
                if (self.addWillData.type.active == 'ER') {
                    if (!self.addWillData.acreage) {
                        hl.toast('请填写需求面积')
                        return false
                    }
                }
                if (self.addWillData.type.active == 'WR') {
                    if (!self.addWillData.wp_count) {
                        hl.toast('请填写需求个数')
                        return false
                    }
                }
                if (!self.addWillData.project_id) {
                    hl.toast('请选择项目')
                    return false
                }
                if (self.addWillData.type.active == 'ER') {
                    if (self.projectInfo.units.length === 0) {
                        hl.toast('请选择意向单元')
                        return false
                    }
                }
                if (self.addWillData.type.active == 'WR') {
                    if (self.projectInfo.wpdata.length === 0) {
                        hl.toast('请选择意向工位')
                        return false
                    }
                }
                return true;
            },
            // 获取招商意向列表
            getCustomerWillsData: function() {
                var self = this;
                hlhttp.get({
                    url: 'customer/wills',
                    data: { customer_id: self.cmrId || '975' },
                    success: function(data) {
                        var index = 0;
                        var willsConfirmedInfoArr = [];
                        var willsListArr = [];
                        self.willsList = data
                            // 单元数据整理
                        data.map(function(_item) {
                            // 整租类型  显示单元
                            if (_item.type === 'ER') {
                                var unitsArr = [];
                                _item.will.map(function(_el) {
                                    if (_el.positions.length === 0) {
                                        var obj = {
                                            'building_id': _el.buildingId,
                                            'building_name': _el.buildingName,
                                            'floor_id': _el.floorId,
                                            'floor_num': _el.floorNum,
                                            'unit_name': _el.unitName,
                                            'unit_id': _el.unitId
                                        }
                                        unitsArr.push(obj)
                                    }
                                })
                                willsConfirmedInfoArr.push({ 'unitData': unitsArr, 'wpData': [] })
                            }
                            // 工位类型  显示工位
                            if (_item.type === 'WR') {
                                var wpArr = [];
                                _item.will.map(function(_el) {
                                    var positionsArr = [];
                                    if (_el.positions.length > 0) {
                                        var obj = {
                                            'building_id': _el.buildingId,
                                            'building_name': _el.buildingName,
                                            'floor_id': _el.floorId,
                                            'floor_num': _el.floorNum,
                                            'unit_id': _el.unitId,
                                            'unit_name': _el.unitName
                                        }
                                        _el.positions.map(function(_el1) {
                                            positionsArr.push({ 'position_id': _el1.positionId, 'position_name': _el1.positionName })
                                        })
                                        obj.positions = positionsArr
                                        wpArr.push(obj)
                                    }
                                })
                                willsConfirmedInfoArr.push({ 'unitData': [], 'wpData': wpArr })
                            }
                            willsListArr.push(_item)
                            willsListArr[index].will = willsConfirmedInfoArr[index]
                            index++;
                        })
                        self.willsConfirmedInfoList = willsConfirmedInfoArr
                        self.addYxIndex = data.length + 1
                    }
                })
            },
            changeDDAccount: function() {
                var self = this;
                if (self.corpConfigFlag == 'Y') self.isShowAccountLayer = true;
                if (self.corpConfigFlag == 'N') self.isShowQyLayer = true;
            },
            validateCorpId: function() {
                var self = this;
                if (!self.corpId) {
                    hl.toast('请填写corpId')
                    $('.corpId-ipt').focus()
                    return false;
                }
            },
            validateCorpSecret: function() {
                var self = this;
                if (self.validateQyForm()) sendValidateRequest()

                function sendValidateRequest() {
                    hlhttp.put({
                        url: 'user/corp_info/_check',
                        data: { 'corp_id': self.corpId, 'corp_secret': self.corpSecret },
                        success: function(data) {
                            hl.toast(data.appDesc)
                        }
                    })
                }
            },
            validateQyForm: function() {
                var self = this;
                if (!self.corpId) {
                    hl.toast('请填写corpId')
                    $('.corpId-ipt').focus()
                    return false;
                }
                if (!self.corpSecret) {
                    hl.toast('请填写corpSecret')
                    $('.corpSecret-ipt').focus()
                    return false;
                }
                return true;
            },
            // 绑定第三方企业信息
            bindQyInfo: function() {
                var self = this;
                if (!self.validateQyForm()) return false;
                hlhttp.post({
                    url: 'user/corp_info/_bind',
                    data: { 'corp_id': self.corpId, 'corp_secret': self.corpSecret },
                    success: function(data) {
                        self.isShowQyLayer = false;
                        self.isShowAccountLayer = true;
                    }
                })
            },
            confirmBindQy: function() {
                this.bindQyInfo()
            },
            closeQyLayer: function() {
                this.isShowQyLayer = false;
            },
            confirmBindAccount: function() {
                var self = this;
                var userIds = null;
                if (customerDDAccount.assigneesData.userIds.length == 0) userIds = '#';
                if (customerDDAccount.assigneesData.userIds.length !== 0) userIds = JSON.stringify(customerDDAccount.assigneesData.userIds);
                if (customerDDAccount.assigneesData.userIds.length == 0) {
                    hl.toast('请选择客户')
                    return false;
                }
                if (customerDDAccount.assigneesData.userIds.length > 1) {
                    hl.toast('只能选择一个客户')
                    return false;
                }

                hlhttp.post({
                    url: 'customer/corp/sync_account',
                    data: { 'user_ids': userIds, 'customer_id': self.cmrId, 'customer_name': self.cmrData.name },
                    success: function(data) {
                        hl.toast('保存成功')
                        self.isShowAccountLayer = false;
                        self.getDDCustomer()
                    }
                })
            },
            closeAccountLayer: function() {
                this.isShowAccountLayer = false;
            },
            getDDCustomer: function() {
                var self = this;
                hlhttp.get({
                    url: 'customer/corp/sync_account',
                    data: { 'customer_id': self.cmrId },
                    success: function(data) {
                        if (JSON.stringify(data) != '{}') self.ddAccount = data.name
                    }
                })
            }
        },
        beforeMount: function() {
            if (window.localStorage.getItem('followUpFlag')) {
                this.followUpFlag = eval(window.localStorage.getItem('followUpFlag'));
                window.localStorage.removeItem('followUpFlag')
            }
            this.corpConfigFlag = JSON.parse(window.localStorage.getItem('userMes')).corpConfigFlag;
            this.isShowddBtn = JSON.parse(window.localStorage.getItem('userMes')).oauthType == 'WX' ? false : true;
            this.ddAccount = JSON.parse(window.localStorage.getItem('userMes')).account;
        },
        mounted: function() {
            this.getType()
            hlSelectUi()
            customerDDAccount.init()
            this.getProjects()
            this.getDDCustomer()
        }
    })
}())
